﻿namespace LogManager.Utils.ChangeTracking
{
    using System;
    using System.ComponentModel;
    using System.Linq.Expressions;

    public abstract class PropertyChangeBase : INotifyPropertyChanged
    {
        public virtual event PropertyChangedEventHandler PropertyChanged = delegate { };

        protected void RaisePropertyChanged<TProperty>(Expression<Func<TProperty>> property)
        {
            RaisePropertyChanged(PropertyName(property));
        }

        public virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected static string PropertyName<T>(Expression<Func<T>> expression)
        {
            var memberExpression = expression.Body as MemberExpression;

            if (memberExpression == null)
            {
                throw new ArgumentException("expression must be a property expression");
            }

            return memberExpression.Member.Name;
        }
    }
}
